package com.sls.web.manager.action.system;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.sys.pojo.OperatorVO;
import com.sls.web.manager.sys.pojo.Role;
import com.sls.web.manager.sys.querybean.OperatorQuery;
import com.sls.web.manager.sys.service.OperatorService;

public class AdminAction extends SuperAction {

	private static final long serialVersionUID = -8335354180204210288L;
	@Resource(name = "operatorService")
	private OperatorService operatorServie;
	/**
	 * 返回分页对象
	 */
	public Pagination<?> page;
	private OperatorVO operator;
	private OperatorQuery operatorQuery;
	private String id;
	private String accountType;
	private List<Role> roleList;

	@JSON(serialize = false)
	public OperatorQuery getOperatorQuery() {
		return operatorQuery;
	}

	public void setOperatorQuery(OperatorQuery operatorQuery) {
		this.operatorQuery = operatorQuery;
	}

	@JSON(serialize = false)
	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	@JSON(serialize = false)
	public OperatorService getOperatorServie() {
		return operatorServie;
	}

	public void setOperatorServie(OperatorService operatorServie) {
		this.operatorServie = operatorServie;
	}

	@JSON(serialize = false)
	public OperatorVO getOperator() {
		return operator;
	}

	public void setOperator(OperatorVO operator) {
		this.operator = operator;
	}

	@JSON(serialize = false)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@JSON(serialize = false)
	public String getAccountType() {
		return accountType;
	}

	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	@JSON(serialize = false)
	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	/*
	 * 打开用户注册页面
	 */
	public String add() {
		operator = new OperatorVO();
		operator.setRole(operatorServie.queryrole(this.getCurrentOperator()));
		return "edit";
	}

	/*
	 * 注册用户
	 */
	public String adduser() throws Exception {
		try {
			operatorServie.regiest(operator, this.getCurrentOperator());
			redirectionUrl = "admin!list.htm";
			return "success";
		} catch (ManagerException e) {
			operator = null;
			addActionError(e.getMessage());
			return this.add();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.add();
		}
	}

	// 列表
	public String list() throws Exception {
		if (operatorQuery == null) {
			operatorQuery = new OperatorQuery();
			operatorQuery.setPageSize(Constants.INIT_PAGESIZE);
			operatorQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = operatorServie.queryOperator(operatorQuery,
					this.getCurrentOperator());
			roleList = operatorServie.queryrole(this.getCurrentOperator());
		} catch (Exception e) {
			return ERROR;
		}

		return "list";
	}

	// 打开操作员
	public String edit() throws Exception {
		if (id != null) {
			operator = operatorServie.queryUserInfoById(id,
					this.getCurrentOperator());
			operator.setRole(operatorServie.queryrole(this.getCurrentOperator()));
		}
		return "edit";
	}

	// 编辑用户
	public String editUser() throws Exception {
		try {
			operatorServie.updateUser(operator, this.getCurrentOperator());
			redirectionUrl = "admin!list.htm";
			return "success";
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return this.edit();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.edit();
		}
	}

	// 校验注册用户是否已存在
	public String checkUsername() throws IOException {
		message = String.valueOf(operatorServie.checkUsername(operator,
				this.getCurrentOperator()));
		return "ajax";
	}

	// 修改密码
	public String pwd() throws Exception {
		try {
			if (operator != null) {
				operatorServie.updatepwd(operator, this.getCurrentOperator());
				redirectionUrl = "admin!pwd.htm";
				return "success";
			} else {
				return "pwd";
			}

		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return "pwd";
		} catch (Exception e) {
			addActionError("系统忙！");
			return "pwd";
		}
	}

	// 注销操作员
	public String cancelUser() throws Exception {
		try {
			operator = new OperatorVO();
			operator.setId(id);
			if (accountType.equals(Constants.LOCK_USER))
				operator.setState(Constants.UPDATE_LOCK);
			else
				operator.setState(Constants.UPDATE_UNLOCK);
			operatorServie.cancelUser(operator, this.getCurrentOperator());
			redirectionUrl = "admin!list.htm";
			return "success";
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return this.list();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.list();
		}
	}
}